package com.itianshan.wheel;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.bluetooth.BluetoothIOClass;
import com.bluetooth.ble.BluetoothLeService;
import com.tencent.liteav.chat.ChatRoomClass;
import com.tencent.liteav.chat.Constant;
import com.tencent.rtmp.ui.TXCloudVideoView;

import java.util.ArrayList;
import java.util.List;

/**
 * RTC视频通话的主页面，只做准备工作
 *
 * 包含如下简单功能：
 * - 进入/退出视频通话房间
 * - 切换前置/后置摄像头
 * - 打开/关闭摄像头
 * - 打开/关闭麦克风
 * - 显示房间内其他用户的视频画面（当前示例最多可显示6个其他用户的视频画面）
 */
public class CloudBaseActivity extends AppCompatActivity implements View.OnClickListener {


    public static final int REQ_PERMISSION_CODE  = 0x1000;
    private static final String TAG = "RTCActivity";
    public boolean                         mIsFrontCamera = true;      // 默认摄像头前置
    private TextView                        roomId;                 //【控件】页面Title
    private TextView                        mTitleText;                 //【控件】页面Title
    public TXCloudVideoView                mLocalPreviewView;          //【控件】本地画面View
    private ImageView                       mBackButton;                //【控件】返回上一级页面
    private Button                          mMuteVideo;                 //【控件】是否停止推送本地的视频数据
    private Button                          mMuteAudio;                 //【控件】开启、关闭本地声音采集并上行
    private Button                          mSwitchCamera;              //【控件】切换摄像头
    private LinearLayout                    mVideoMutedTipsView;        //【控件】关闭视频时，显示默认头像
    private Button                          mLogInfo;                   //【控件】开启、关闭日志显示
    private int                             mLogLevel = 0;              // 日志等级

    public ChatRoomClass                   chatRoomClass;               // 房间类
    public String                          mRoomId;                    // 房间Id
    public String                          mUserId;                    // 用户Id
    public List<TXCloudVideoView>          mRemoteViewList;            // 远端画面列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cloud_car);
        //getSupportActionBar().hide();
        Intent intent = getIntent();
        if (null != intent) {
            if (intent.getStringExtra(Constant.USER_ID) != null) {
                mUserId = intent.getStringExtra(Constant.USER_ID);
            }
            if (intent.getStringExtra(Constant.ROOM_ID) != null) {
                mRoomId = intent.getStringExtra(Constant.ROOM_ID);
            }
        }
        //chatRoomClass.enterRoom(getApplicationContext());
    }





    public void initView() {
        //mTitleText          = findViewById(R.id.trtc_tv_room_number);
        mBackButton         = findViewById(R.id.trtc_ic_back);
        mLocalPreviewView   = findViewById(R.id.trtc_tc_cloud_view_main);
        mMuteVideo          = findViewById(R.id.trtc_btn_mute_video);
        mMuteAudio          = findViewById(R.id.trtc_btn_mute_audio);
        mSwitchCamera       = findViewById(R.id.trtc_btn_switch_camera);
        mLogInfo            = findViewById(R.id.trtc_btn_log_info);
        mVideoMutedTipsView = findViewById(R.id.ll_trtc_mute_video_default);
        //roomId = findViewById(R.id.trtc_tv_room_number);

        //if (!TextUtils.isEmpty(mRoomId)) {
        //    mTitleText.setText(mRoomId);
        //}
        mBackButton.setOnClickListener(this);
        mMuteVideo.setOnClickListener(this);
        mMuteAudio.setOnClickListener(this);
        mSwitchCamera.setOnClickListener(this);
        mLogInfo.setOnClickListener(this);

        mRemoteViewList = new ArrayList<>();
        mRemoteViewList.add((TXCloudVideoView)findViewById(R.id.trtc_tc_cloud_view_1));
        mRemoteViewList.add((TXCloudVideoView)findViewById(R.id.trtc_tc_cloud_view_2));
        mRemoteViewList.add((TXCloudVideoView)findViewById(R.id.trtc_tc_cloud_view_3));
        mRemoteViewList.add((TXCloudVideoView)findViewById(R.id.trtc_tc_cloud_view_4));
        mRemoteViewList.add((TXCloudVideoView)findViewById(R.id.trtc_tc_cloud_view_5));
        mRemoteViewList.add((TXCloudVideoView)findViewById(R.id.trtc_tc_cloud_view_6));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        chatRoomClass.exitRoom();
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//恢复竖屏
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.trtc_ic_back) {
            finish();
        } else if (id == R.id.trtc_btn_mute_video) {
            muteVideo();
        } else if (id == R.id.trtc_btn_mute_audio) {
            chatRoomClass.toggleRemoteViewRotation();

            muteAudio();
        } else if (id == R.id.trtc_btn_switch_camera) {
            switchCamera();
        } else if (id == R.id.trtc_btn_log_info) {
            showDebugView();
        }
    }

    public void muteVideo() {
        boolean isSelected = mMuteVideo.isSelected();
        if (!isSelected) {
            chatRoomClass.mTRTCCloud.stopLocalPreview();
            mMuteVideo.setBackground(getResources().getDrawable(R.mipmap.rtc_camera_off));
            mVideoMutedTipsView.setVisibility(View.VISIBLE);
        } else {
            chatRoomClass.mTRTCCloud.startLocalPreview(mIsFrontCamera, mLocalPreviewView);
            mMuteVideo.setBackground(getResources().getDrawable(R.mipmap.rtc_camera_on));
            mVideoMutedTipsView.setVisibility(View.GONE);
        }
        mMuteVideo.setSelected(!isSelected);
    }

    public void muteAudio() {
        boolean isSelected = mMuteAudio.isSelected();
        if (!isSelected) {
            chatRoomClass.mTRTCCloud.stopLocalAudio();
            mMuteAudio.setBackground(getResources().getDrawable(R.mipmap.rtc_mic_off));
        } else {
            chatRoomClass.mTRTCCloud.startLocalAudio();
            mMuteAudio.setBackground(getResources().getDrawable(R.mipmap.rtc_mic_on));
        }
        mMuteAudio.setSelected(!isSelected);
    }

    public void switchCamera() {
        chatRoomClass.mTRTCCloud.switchCamera();
        boolean isSelected = mSwitchCamera.isSelected();
        mIsFrontCamera = !isSelected;
        mSwitchCamera.setSelected(!isSelected);
    }

    public void showDebugView() {
        if(chatRoomClass.isInTheRoom()) {
            mLogLevel = (mLogLevel + 1) % 3;
            chatRoomClass.mTRTCCloud.showDebugView(mLogLevel);
        }
    }
    //////////////////////////////////    Android动态权限申请   ////////////////////////////////////////

    public boolean checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            List<String> permissions = new ArrayList<>();
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)) {
                permissions.add(Manifest.permission.CAMERA);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)) {
                permissions.add(Manifest.permission.RECORD_AUDIO);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
            }
            if (permissions.size() != 0) {
                ActivityCompat.requestPermissions(this,
                        permissions.toArray(new String[0]),
                        REQ_PERMISSION_CODE);
                return false;
            }
        }
        return true;
    }
}
